package com.pphh.demo;

import com.alibaba.dubbo.rpc.RpcContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Please add description here.
 *
 * @author huangyinhuang
 * @date 11/12/2018
 */
public class Consumer {

    private static final Logger logger = LogManager.getLogger();

    public static void main(String[] args) {
        logger.info("this is a log test message");

        //Prevent to get IPV6 address,this way only work in debug mode
        //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();
        // get remote service proxy
        DemoService demoService = (DemoService) context.getBean("demoService");

        while (true) {
            try {
                String localAddress = RpcContext.getContext().getLocalAddressString();
                String remoteAddress = RpcContext.getContext().getRemoteAddressString();
                System.out.println("consumer address: " + localAddress + "--> " + "provider address: " + remoteAddress);
                Thread.sleep(1000);
                String hello = demoService.sayHello("world");
                System.out.println(hello);
                System.out.println();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }

    }
}
